// packageStore/pages/purchase/dealing_offline/dealing_offline.js
import {api, storage} from '../../../../js/util';
import {dealing} from '../../../../js/modules/market';
import { router } from '../../../../js/router';
var app = getApp();
import BasePage from '../../../../js/common/base-page'
import { config } from "../../../../js/modules/gloab.js";
BasePage({
  pageKey:"dealingOffline",
  /**
   * 页面的初始数据
   */
  data: {
    tablist: ['本周','本月','上月','近三月', '所有'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 70,
    headerBg:'',
    lists:[
      {
        time: 'Week',  //本周
        list: [],
        totle:{},
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
        }
      },
      {
        time: 'CurrentMonth',  //本月
        list: [],
        totle:{},
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
        }
      },
      {
        time: 'LastMonth',  //上月
        list: [],
        totle:{},
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
        }
      },
      {
        time: 'NearlyThreeMonth',  //近三月
        list: [],
        totle:{},
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
        }
      },
      {
        time: 'All',  //所有
        list: [],
        totle:{},
        hasMoreData: true,
        page: {
          PageIndex: 1,
          PageSize: 20,
        }
      }
    ],
    Currency:'',//货币符号
    mainColor:api.titleColor||"#e56b14",//主题色
    orderTypeList: ['线上', '全部'],
    selectedOrderTypeIndex: 1,
    quantityDigits: 2, // 数量显示小数位数
    priceDigits: 2, // 价格显示小数位数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getConfig()
    this.loadList()
  },
  loadList:function(){
    var that = this;
    var current = this.data.lists[that.data.activeIndex];
    var param = current.page;
    dealing.getOfflineData(current.time, param, function (content) {
      var hasMoreData = content.count > (current.page.PageIndex) * current.page.PageSize;
      var list_key = "lists[" + that.data.activeIndex + "].list";
      var totle_key = "lists[" + that.data.activeIndex + "].totle";
      //是否有更多数据
      var list_has = "lists[" + that.data.activeIndex + "].hasMoreData";
      //无数据时显示的信息
      var isLoadComplete = "lists[" + that.data.activeIndex + "].isLoadComplete";
      var data = content.data;
      //当分页大于1时，累加
      if (current.page.PageIndex > 1) {
        data = that.data.lists[that.data.activeIndex].list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        [list_key]: data,
        [totle_key]: content.properties,
        [list_has]: hasMoreData,
        [isLoadComplete]:true
      });

    })
  },
  /**
   * 切换当前选择项
   */
  onSwitchItem: function (res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex
    });
    var current = this.data.lists[activeIndex];
    this.loadList();

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg: api.titleColor||"#e56b14",
      Currency:api.Currency || '￥'
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.lists[this.data.activeIndex].page.PageIndex = 1;
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var current = this.data.lists[this.data.activeIndex];
    if (current.hasMoreData) {
      var index = this.data.lists[this.data.activeIndex].page.PageIndex;
      this.data.lists[this.data.activeIndex].page.PageIndex = index + 1;
      this.loadList();
    }
  },

  showOrderTypeList() {
    this.setData({
      showOrderType: true
    })
  },
  selectOrderType(e) {
    let index = e.currentTarget.dataset.index
    if (index == 0) {
      this.setData({
        showOrderType: false
      })
      storage.put('page_dealing_type', 'online', 0)
      router.redirect({
        path: 'dealing'
      })
    }
  },

  toOrderDetail (e) {
    let item = e.currentTarget.dataset.item || {}
    // 支付无订单详情
    if (item.billType == '支付') {
      return
    }
    if (item.billTypeId == 45) {
      // 退货订单
      router.navigate({
        path: 'orderOffineRetreat',
        params: {
          oddNumber: item.BillCode
        }
      })
    } else if (item.billTypeId == 11) {
      // 采购订单
      router.navigate({
        path: 'orderOffine',
        params: {
          oddNumber: item.BillCode
        }
      })
    }
  },

  getConfig () {
    let that = this
    let fkid = app.dns.proprietorId
    let fkflag = app.dns.proprietor
    config.getWholesaleConfigs(
    fkid,
    fkflag,
    'Wholesale',
      (model)=> {
        that.setData({
          quantityDigits: model.QuantityDigits,
          priceDigits: model.PriceDigits
        })
    })
  }
})